/**
 * 
 */
package gui;

import general.VariablesGlobales;

import io.ImporterFichierHTML;
import io.OutilsParsingHTML;

import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Event;
import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.SwingUtilities;
import javax.swing.KeyStroke;
import java.awt.Point;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;

import classificationEtSelection.ParametresClassifications;

/**
 * @author remi
 *@version 1.0
 */
public class Application {

	private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="10,10"
	private JPanel jContentPane = null;
	private JMenuBar jJMenuBar = null;
	private JMenu fileMenu = null;
	private JMenu editMenu = null;
	private JMenu helpMenu = null;
	private JMenuItem exitMenuItem = null;
	private JMenuItem aboutMenuItem = null;
	private JMenuItem cutMenuItem = null;
	private JMenuItem copyMenuItem = null;
	private JMenuItem pasteMenuItem = null;
	private JMenuItem saveMenuItem = null;
	private static Application application= null;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Application application = new Application();
				application.getJFrame().setVisible(true);
				application.getJFrame().pack();
			}
		});
	}



	/**
	 * Constructeur classique
	 */
	public Application() {
		super();
		application = this;
	}


	/**
	 * fonction permettant de recuperer l'instance de application depuis n'importe ou dans le programme
	 * @return l'instance d'application
	 */
	public static Application getInstance(){
		return Application.application;
	}

	public JFrame getFrame(){
		return this.jFrame;
	}

	/**
	 * This method initializes jFrame
	 * 
	 * @return javax.swing.JFrame
	 */
	private JFrame getJFrame() {
		if (jFrame == null) {
			jFrame = new JFrame();
			jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			jFrame.setJMenuBar(getJJMenuBar());
			jFrame.setSize(818, 277);
			jFrame.setContentPane(getJContentPane());
			jFrame.setTitle("DBWeb2010 projet");
		}
		return jFrame;
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	/**
	 * @return
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new GridLayout(1,3));

			JPanel panneauxMilieu = new JPanel();
			panneauxMilieu.setLayout(new GridLayout(8, 1));

			JButton selectionnerRepertoireSource = new JButton("Selectionner Repertoire source");

			JButton selectionnerRepertoireDestinationClassification  = new JButton("Selectionner repertoire de sortie de classification");

			JButton selectionnerRepertoireDestinationNettoyage = new JButton("Selectionner le repertoire de sortie du nettoyage");

			JButton lancerExtraction = new JButton("lancer l'extraction");		
			JButton lancerClassification = new JButton("lancer la classification");
			JButton lancerCopieFichier = new JButton("lancer copie clusters sur disque");
			JButton lancerNettoyage = new JButton("lancer le nettoyage");

			panneauxMilieu.add(selectionnerRepertoireSource);
			panneauxMilieu.add(selectionnerRepertoireDestinationClassification);
			panneauxMilieu.add(selectionnerRepertoireDestinationNettoyage);
			panneauxMilieu.add(lancerExtraction);
			panneauxMilieu.add(lancerClassification);
			panneauxMilieu.add(lancerCopieFichier);
			panneauxMilieu.add(lancerNettoyage);




			// creation du JPAnnel de gauche avec la zone de texte
			JPanel panneauxGauche = new JPanel();
			panneauxGauche.setLayout(new BorderLayout());

			TextArea textArea = new TextArea();
			panneauxGauche.add(textArea,BorderLayout.NORTH);

			// creation du JPannel de droite avec les slidders

			JPanel panneauxDroite = new JPanel();
			panneauxDroite.setLayout(new GridLayout(4,1));



			//creation des sliders
			final JSlider slider1 = new JSlider(0, 100);
			final JSlider slider2 = new JSlider(0, 100);
			final JSlider slider3 = new JSlider(0, 100);
			final JSlider slider4 = new JSlider(0, 100);
			final JSlider slider5 = new JSlider(1, 100);
			final JSlider slider6 = new JSlider(0, 50);

			slider1.setMajorTickSpacing(10);
			slider1.setMinorTickSpacing(5);
			slider1.setPaintTicks(true);
			slider1.setPaintLabels(true);

			slider2.setMajorTickSpacing(10);
			slider2.setMinorTickSpacing(5);
			slider2.setPaintTicks(true);
			slider2.setPaintLabels(true);

			slider3.setMajorTickSpacing(10);
			slider3.setMinorTickSpacing(5);
			slider3.setPaintTicks(true);
			slider3.setPaintLabels(true);

			slider4.setMajorTickSpacing(10);
			slider4.setMinorTickSpacing(5);
			slider4.setPaintTicks(true);
			slider4.setPaintLabels(true);

			slider5.setMajorTickSpacing(10);
			slider5.setMinorTickSpacing(5);
			slider5.setPaintTicks(true);
			slider5.setPaintLabels(true);

			slider6.setMajorTickSpacing(10);
			slider6.setMinorTickSpacing(5);
			slider6.setPaintTicks(true);
			slider6.setPaintLabels(true);

			VariablesGlobales.initialisationPonderation();
			slider1.setValue((int) (VariablesGlobales.getPonderationCriteres().get(0) * 100));
			slider2.setValue((int) (VariablesGlobales.getPonderationCriteres().get(1) * 100));
			slider3.setValue((int) (VariablesGlobales.getPonderationCriteres().get(2) * 100));
			slider4.setValue((int) (VariablesGlobales.getPonderationCriteres().get(3) * 100));
			slider5.setValue((int) (ParametresClassifications.seuilClassification * 30));
			slider6.setValue((int) (ParametresClassifications.seuilSelectionVarMax));

			slider1.setBorder(BorderFactory.createTitledBorder("Crit�re DCT"));
			slider2.setBorder(BorderFactory.createTitledBorder("Crit�re Depth"));
			slider3.setBorder(BorderFactory.createTitledBorder("Crit�re TagP"));
			slider4.setBorder(BorderFactory.createTitledBorder("Crit�re URL"));
			slider5.setBorder(BorderFactory.createTitledBorder("Diam�tre maximum des clusters"));
			slider6.setBorder(BorderFactory.createTitledBorder("Variance maximum des clusters selectionn�s"));

			panneauxDroite.add(slider1);
			panneauxDroite.add(slider2);
			panneauxDroite.add(slider3);
			panneauxDroite.add(slider4);
			panneauxDroite.add(slider5);
			panneauxDroite.add(slider6);

			//remplissage du JContentPane
			jContentPane.add(panneauxGauche);
			jContentPane.add(panneauxMilieu);
			jContentPane.add(panneauxDroite);


			//actions

			selectionnerRepertoireSource
			.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					// lancer la selection du fichier
					Controle.actionSelectionRepertoireSource();				}
			});
			selectionnerRepertoireDestinationClassification.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					// lancer la selection du fichier
					Controle.actionSelectionRepertoireDestinationClassification();				}
			});

			selectionnerRepertoireDestinationNettoyage.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					// lancer la selection du fichier
					Controle.actionSelectionRepertoireDestinationNettoyage();				}
			});


			lancerExtraction.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					// lancer l'extraction
					Controle.actionLancerExtraction();				}
			});
			lancerCopieFichier.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					// lancer la copie des clusters sru le disque
					Controle.actionLancerCopieClusters();				}
			});

			lancerClassification.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					// lancer la selection du fichier
					Controle.actionLancerClassification();				}
			});


			lancerNettoyage.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					// lancer la selection du fichier
					Controle.actionLancerNettoyage();				}
			});



			/*
			 * Met � jour les ponderations pour chacun des criteres
			 */
			slider1.addMouseListener(new java.awt.event.MouseAdapter(){
				public void mouseReleased(java.awt.event.MouseEvent e) {
					// modification de la ponderation critere DCT
					VariablesGlobales.initialisationPonderation();
					int val = slider1.getValue() + slider2.getValue() + slider3.getValue() + slider4.getValue() + 1;
					VariablesGlobales.setPonderationCritereDCT((float)slider1.getValue()/val*4);
					VariablesGlobales.setPonderationCritereDepth((float)slider2.getValue()/val*4);
					VariablesGlobales.setPonderationCritereTagP((float)slider3.getValue()/val*4);
					VariablesGlobales.setPonderationCritereURL((float)slider4.getValue()/val*4);

				}
			});

			/*
			 * Met � jour les ponderations pour chacun des criteres
			 */
			slider2.addMouseListener(new java.awt.event.MouseAdapter(){
				public void mouseReleased(java.awt.event.MouseEvent e) {
					// modification de la ponderation critere Depth
					VariablesGlobales.initialisationPonderation();
					int val = slider1.getValue() + slider2.getValue() + slider3.getValue() + slider4.getValue() + 1;
					VariablesGlobales.setPonderationCritereDCT((float)slider1.getValue()/val*4);
					VariablesGlobales.setPonderationCritereDepth((float)slider2.getValue()/val*4);
					VariablesGlobales.setPonderationCritereTagP((float)slider3.getValue()/val*4);
					VariablesGlobales.setPonderationCritereURL((float)slider4.getValue()/val*4);				}
			});

			/*
			 * Met � jour les ponderations pour chacun des criteres
			 */
			slider3.addMouseListener(new java.awt.event.MouseAdapter(){
				public void mouseReleased(java.awt.event.MouseEvent e) {
					// modification de la ponderation critere TagP
					VariablesGlobales.initialisationPonderation();
					int val = slider1.getValue() + slider2.getValue() + slider3.getValue() + slider4.getValue()+ 1;
					VariablesGlobales.setPonderationCritereDCT((float)slider1.getValue()/val*4);
					VariablesGlobales.setPonderationCritereDepth((float)slider2.getValue()/val*4);
					VariablesGlobales.setPonderationCritereTagP((float)slider3.getValue()/val*4);
					VariablesGlobales.setPonderationCritereURL((float)slider4.getValue()/val*4);				}
			});

			/*
			 * Met � jour les ponderations pour chacun des criteres
			 */
			slider4.addMouseListener(new java.awt.event.MouseAdapter(){
				public void mouseReleased(java.awt.event.MouseEvent e) {
					// modification de la ponderation critere URL
					VariablesGlobales.initialisationPonderation();
					int val = slider1.getValue() + slider2.getValue() + slider3.getValue() + slider4.getValue()+ 1;
					VariablesGlobales.setPonderationCritereDCT((float)slider1.getValue()/val*4);
					VariablesGlobales.setPonderationCritereDepth((float)slider2.getValue()/val*4);
					VariablesGlobales.setPonderationCritereTagP((float)slider3.getValue()/val*4);
					VariablesGlobales.setPonderationCritereURL((float)slider4.getValue()/val*4);				}
			});



			/*
			 * Permet de selectionner le diametre maximum des clusters
			 */
			slider5.addMouseListener(new java.awt.event.MouseAdapter(){
				public void mouseReleased(java.awt.event.MouseEvent e) {
					// modification du diametre maximum pour les clusters
					ParametresClassifications.seuilClassification = (float)slider5.getValue()/30;
				}
			});
			/*
			 * Permet de selectionner le seuil de variabilite maximum pour qu'un cluster soit selectionne
			 */
			slider6.addMouseListener(new java.awt.event.MouseAdapter(){
				public void mouseReleased(java.awt.event.MouseEvent e) {
					// modification du seuil de variabilite maximum pour qu'un cluster soit selectionne
					ParametresClassifications.seuilSelectionVarMax = (float) slider6.getValue();
				}
			});


		}
		return jContentPane;
	}

	/**
	 * This method initializes jJMenuBar	
	 * 	
	 * @return javax.swing.JMenuBar	
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getFileMenu());
			jJMenuBar.add(getEditMenu());
			jJMenuBar.add(getHelpMenu());
		}
		return jJMenuBar;
	}

	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getFileMenu() {
		if (fileMenu == null) {
			fileMenu = new JMenu();
			fileMenu.setText("File");
			fileMenu.add(getSaveMenuItem());
			fileMenu.add(getExitMenuItem());
		}
		return fileMenu;
	}

	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getEditMenu() {
		if (editMenu == null) {
			editMenu = new JMenu();
			editMenu.setText("Edit");
			editMenu.add(getCutMenuItem());
			editMenu.add(getCopyMenuItem());
			editMenu.add(getPasteMenuItem());
		}
		return editMenu;
	}

	/**
	 * This method initializes jMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getHelpMenu() {
		if (helpMenu == null) {
			helpMenu = new JMenu();
			helpMenu.setText("Help");
			helpMenu.add(getAboutMenuItem());
		}
		return helpMenu;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getExitMenuItem() {
		if (exitMenuItem == null) {
			exitMenuItem = new JMenuItem();
			exitMenuItem.setText("Exit");
			exitMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			});
		}
		return exitMenuItem;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getAboutMenuItem() {
		if (aboutMenuItem == null) {
			aboutMenuItem = new JMenuItem();
			aboutMenuItem.setText("About");
			
			aboutMenuItem.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					// lancer la selection du fichier
					//lancer l'affichage d'un popu d'aide

					
					String texte = "";
					System.out.println("on a lciqué sur about");
				
					JOptionPane.showMessageDialog(Application.getInstance().getJContentPane(), "Ce n'est qu'un simple message",
						      "avertissement",
						      JOptionPane.WARNING_MESSAGE);
					
//					jop.showMessageDialog(Application.getInstance(), "Bonjour, pour utiliser ce programme normalement :" +
//							"\n 1 : Description du programme  : ce programme va chercher a detecter les sources structurée parmis un ensemble de page html. P" +
//							"ar source structurées on entend des pages qui on le meme genre de structure html \n" +
//							"pour plus d'information, se referer au google code du projet : ");

				}
			});


			aboutMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.out.println("clique sur about 2");
					
					JOptionPane jop = new JOptionPane(Application.getInstance().getJContentPane());
					jop.setAutoscrolls(true);
					
					JTextArea area = new JTextArea(OutilsParsingHTML.fileToString(new File("doc/README.txt")) );
			        area.setRows(20);
			        area.setColumns(100);
			        area.setLineWrap(true);
			        JScrollPane pane = new JScrollPane(area);
			        //
			        JOptionPane.showConfirmDialog(null, pane, "Test",
			            JOptionPane.CLOSED_OPTION, JOptionPane.INFORMATION_MESSAGE);


					
					
					//					JDialog aboutDialog = getAboutDialog();
					//					aboutDialog.pack();
					Point loc = getJFrame().getLocation();
					loc.translate(20, 20);
					//					aboutDialog.setLocation(loc);
					//					aboutDialog.setVisible(true);
				}
			});
		}
		return aboutMenuItem;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getCutMenuItem() {
		if (cutMenuItem == null) {
			cutMenuItem = new JMenuItem();
			cutMenuItem.setText("Cut");
			cutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
					Event.CTRL_MASK, true));
		}
		return cutMenuItem;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getCopyMenuItem() {
		if (copyMenuItem == null) {
			copyMenuItem = new JMenuItem();
			copyMenuItem.setText("Copy");
			copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
					Event.CTRL_MASK, true));
		}
		return copyMenuItem;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getPasteMenuItem() {
		if (pasteMenuItem == null) {
			pasteMenuItem = new JMenuItem();
			pasteMenuItem.setText("Paste");
			pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,
					Event.CTRL_MASK, true));
		}
		return pasteMenuItem;
	}

	/**
	 * This method initializes jMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getSaveMenuItem() {
		if (saveMenuItem == null) {
			saveMenuItem = new JMenuItem();
			saveMenuItem.setText("Save");
			saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
					Event.CTRL_MASK, true));
		}
		return saveMenuItem;
	}

}
